﻿namespace Nomo.UnityCoreModule.Nodes
{
    /// <summary>
    /// 根界面的容器。
    /// </summary>
    [UnityEngine.DisallowMultipleComponent]
    [UnityEngine.RequireComponent(typeof(UnityEngine.RectTransform))]
    public sealed class NodeRoot : UnityEngine.EventSystems.UIBehaviour
    {
        internal readonly System.Collections.Generic.List<Node> RootNodes = new System.Collections.Generic.List<Node>();

        /// <inheritdoc />
        protected override void Awake()
        {
            base.Awake();
            RefreshRootNodes();
        }

        private void OnTransformChildrenChanged()
        {
            RefreshRootNodes();
        }

        private void RefreshRootNodes()
        {
            RootNodes.Clear();
            RootNodes.AddRange(System.Linq.Enumerable.Where(System.Linq.Enumerable.Select(System.Linq.Enumerable.Cast<UnityEngine.Transform>(transform), e => e.GetComponent<Node>()), e => e != null));
        }
    }
}
